<template>
  <el-dialog title="修改密码"
             :visible.sync="dialogFormVisible2"
             :close-on-click-modal="true"
             :close-on-press-escape="false"
             :fullscreen="false"
             :append-to-body="true"
             width="40%"
  >
    <el-form ref="dataForm"
             :rules="rules"
             :model="formData"
             label-width="130px">
      <el-form-item label="旧密码" prop="oldPassword">
        <el-input v-model="formData.oldPassword" />
      </el-form-item>
      <el-form-item label="新密码" prop="newPassword1">
        <el-input v-model="formData.newPassword1" />
      </el-form-item>
      <el-form-item label="确认新密码" prop="newPassword2">
        <el-input v-model="formData.newPassword2" />
      </el-form-item>

      <el-row class="dialog-btn">
        <el-col :span="24">
          <el-form-item class="dialog-btn-private">
            <el-button type="info"
                       @click="dialogFormVisible2 = false"
                       size="mini">取消</el-button>
            <el-button type="primary"
                       size="mini"
                       @click="updateData()">确认</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </el-dialog>
</template>

<script>
import { MessageBox } from 'element-ui'
import * as apiUser from '@/api/user'

export default {
  name: 'ChangePassword',
  props: ['dialogFormVisible'],
  computed: {
    dialogFormVisible2: {
      get () {
        return this.dialogFormVisible
      },
      set (val) {
        // dialogFormVisible改变由父组件控制
        this.$emit('update:dialogFormVisible', val)
      }
    }
  },
  created () {
  },
  data () {
    return {
      formData: {
        id: undefined,
        tenantId: undefined,
        oldPassword: '',
        newPassword1: '',
        newPassword2: ''
      },
      rules: {
        oldPassword: [{ required: true, message: '旧密码必填', trigger: 'blur' }],
        newPassword1: [{ required: true, message: '新密码必填', trigger: 'blur' }],
        newPassword2: [{ required: true, message: '新密码必填', trigger: 'blur' }]
      }
    }
  },
  methods: {
    clearFormData () {//清除 this.formData数据
      Object.keys(this.formData).forEach(key => {
        this.formData[key] = undefined
      })
    },
    setCurrentRow (currentRowData) {// 修改 某条记录时 获取记录信息
      this.formData = Object.assign({}, currentRowData)
    },
    updateData () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const tempData = Object.assign({}, this.formData)
          if(this.formData.newPassword1 !== this.formData.newPassword2) {
            MessageBox('两次新密码不一致')
            return
          }
          tempData.newPassword = this.formData.newPassword1
          apiUser.changePassword(tempData).then(() => {
            // 通知父组件刷新列表数据
            this.dialogFormVisible2 = false
            this.$notify({
              title: '成功',
              message: '修改成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    }
  }
}
</script>

<style scoped>
  .dialog-btn-private {
    position: relative;
    left: -50px;
  }
</style>
